package org.jiaxin.nutrient.controller;

import org.jiaxin.nutrient.bean.UserInfo;
import org.jiaxin.nutrient.utils.CommonUtils;
import org.jiaxin.nutrient.utils.DBTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiaxin
 * @version 1.0
 * @description: TODO 用户登陆逻辑的编写
 * @date 2024/3/20 22:36
 */
@RestController
public class UserLoginController {
    private Logger logger= LoggerFactory.getLogger(UserLoginController.class);

    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody UserInfo userInfo) throws Exception {
        //用户登陆时的判断 如果数据库中存在user 那么返回前端 存在的信息，如果不存在，则返回不存在，并且让前端进入注册界面
        System.out.println("用户登陆成功 montherfucker "+userInfo);
        String sql="select * from user_info where user_name='${user_name}' and role = '${user_role}' and pass_word='${pass_word}'";
        sql=sql.replace("${user_name}",userInfo.getUserName())
                .replace("${pass_word}",userInfo.getPassWord())
                .replace("${user_role}",userInfo.getRole());
        System.out.println(sql);
        List<UserInfo> sqlData = null;
        DBTemplate<UserInfo> instance = DBTemplate.getInstance();
        Map<String, Object> hashMap = new HashMap<>();
        int status;
        try {
            sqlData= instance.getSqlData(sql, UserInfo.class);
            instance.close();
            status=200;
        }catch (Exception e){
            e.printStackTrace();
            status=500;
        }

        assert sqlData != null;
        if (status==200){
            if (sqlData.isEmpty()){
                System.out.println("输入的账号或者密码不正确，请重新输入！");
                status=100;
                hashMap.put("status",status);
                hashMap.put("message","输入的账号或者密码不正确，请重新输入！");
            }else {
                System.out.println("输入的账号密码存在！！！");
                hashMap.put("status",status);
                hashMap.put("message","登陆成功！");
            }

        }else {
            System.out.println("输入的账号或者密码不正确，请重新输入！");
            hashMap.put("status",status);
            hashMap.put("message","输入的账号或者密码不正确，查询失败！请重新输入！");
        }
        instance.close();
        return hashMap;
    }
    @PostMapping("/register")
    public Map<String,Object> register(@RequestBody UserInfo userInfo) throws Exception {
        //注册用户信息
        Map<String, Object> hashMap = new HashMap<>();
        int status;
        try {
            DBTemplate<UserInfo> instance = DBTemplate.getInstance();
            //System.out.println(userInfo);
            logger.info("{} "+userInfo,"注册的用户名称是: ");
            //用户 姓名跟学号 不能跟数据库中的重复 且是唯一的 即 姓名+学号 是唯一的主键
            String sql="select * from user_info where user_name='${user_name}'";
            sql=sql.replace("${user_name}",userInfo.getUserName());

            List<UserInfo> sqlData = instance.getSqlData(sql, UserInfo.class);
            if (sqlData.isEmpty()){
                instance.save("user_info",UserInfo.class,userInfo,true);
                status=200;
                hashMap.put("status",status);
                hashMap.put("message","注册成功！！");
            }else {
                status=400;
                hashMap.put("status",status);
                hashMap.put("message","用户名重复！请重新输入用户名！！！");
            }
            instance.close();
        }catch (Exception e){
            e.printStackTrace();
            status=500;
            hashMap.put("status",status);
            hashMap.put("message","注册失败！！！，请重新输入！！");
        }

        return hashMap;
    }

    @RequestMapping("/admin/showAll")
    public Map<String,Object> showAll() throws Exception {
        Map<String, Object> hashMap = new HashMap<>();
        DBTemplate<UserInfo> instance = DBTemplate.getInstance();
        List<UserInfo> allData = instance.getTableAllData("user_info", UserInfo.class);
        Long tableRowNum = instance.getTableRowNum("select count(*) from user_info");
        hashMap.put("total",tableRowNum);
        hashMap.put("data",allData);
        hashMap.put("status",200);
        instance.close();
        return hashMap;
    }
    @RequestMapping("/admin/delete")
    public Map<String,Object> deleteUserInfo(@RequestParam Integer id) {
        try {
            DBTemplate<UserInfo> instance = DBTemplate.getInstance();
            String sql="delete  from user_info where id="+id;
            instance.doExecuteSql(sql);
            return CommonUtils.returnMap("删除成功！！",200);
        }catch (Exception e){
            return CommonUtils.returnMap(e.getMessage(),300);
        }
    }

}
